package com.pipayshop.utils;

import com.pipayshop.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lzy
 * @create 2024/12/13 15:29
 */
public class ValidationUtil {

    // 校验字符串不能为空
    public static void validateString(String value, String errorMessage) {
        if (StringUtils.isBlank(value)) {
            throw new BusinessException(errorMessage);
        }
    }
    // 校验对象是否为空
    public static void validateNotNull(Object value, String errorMessage) {
        if (value == null) {
            throw new BusinessException(errorMessage);
        }
    }
    // 校验数字是否大于零
    public static void validatePositiveNumber(Number value, String errorMessage) {
        if (value == null || value.doubleValue() <= 0) {
            throw new BusinessException(errorMessage);
        }
    }
    // 校验数字是否大于 0
    public static void validatePositiveNumber(Integer value, String errorMessage) {
        if (value == null || value <= 0) {
            throw new BusinessException(errorMessage);
        }
    }
    // 校验 BigDecimal 是否大于零
    public static void validatePositiveBigDecimal(BigDecimal value, String errorMessage) {
        if (value == null || value.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(errorMessage);
        }
    }

    // 校验分页参数
    public static void validatePageParams(Integer start, Integer size, String pageNumberError, String pageSizeError) {
        if (start == null || start < 1) {
            throw new BusinessException(pageNumberError);
        }
        if (size == null || size < 1) {
            throw new BusinessException(pageSizeError);
        }
    }
    // 校验 List 是否为空
    public static void validateListNotEmpty(List<?> list, String errorMessage) {
        if (list == null || list.isEmpty()) {
            throw new BusinessException(errorMessage);
        }
    }
    // 校验数字是否在指定范围内
    public static void validateScore(Double score, double min, double max, String errorMessage) {
        if (score == null || score < min || score > max) {
            throw new BusinessException(errorMessage);
        }
    }

}
